// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------
// Modification history:
// feature: Unified Transport Abstraction Layer Unified Transport Abstraction Layer
// ------------------------------------------------------------------

#ifndef SRC_CPP_RTPS_TRANSPORT_CHANNELRESOURCE_H_
#define SRC_CPP_RTPS_TRANSPORT_CHANNELRESOURCE_H_

#include <memory>
#include <map>
#include <elog/Log.hpp>

namespace vbs {
namespace transport {

class ChannelResource {
 public:
    ChannelResource();
    ChannelResource(ChannelResource&& channelResource);
    ChannelResource(uint32_t rec_buffer_size);
    virtual ~ChannelResource();

    virtual void clear();

    inline void thread(std::thread&& pThread) {
        if (thread_.joinable()) {
            thread_.join();
        }

        thread_.swap(pThread);
    }

    inline bool alive() const { return alive_.load(); }

    inline virtual void disable() { alive_.store(false); }

    inline unsigned char* message_buffer() { return message_buffer_; }
    inline uint32_t buffer_size() { return buffer_size_; }

 protected:
    //! Received message
    unsigned char* message_buffer_;
    uint32_t buffer_size_ = 0;

    std::atomic<bool> alive_;
    std::thread thread_;
};

}  // namespace transport
}  // namespace vbs

#endif  // SRC_CPP_RTPS_TRANSPORT_CHANNELRESOURCE_H_
